<template>
	<view class="user-create">
		<uni-card title="新增用户">
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-forms-item label="用户名" name="username">
					<uni-easyinput v-model="formData.username" placeholder="请输入用户名" />
				</uni-forms-item>

				<uni-forms-item label="密码" name="password">
					<uni-easyinput v-model="formData.password" type="password" placeholder="请输入密码" />
				</uni-forms-item>

				<uni-forms-item label="邮箱" name="email">
					<uni-easyinput v-model="formData.email" type="email" placeholder="请输入邮箱" />
				</uni-forms-item>

				<uni-forms-item label="手机号" name="phone">
					<uni-easyinput v-model="formData.phone" type="number" placeholder="请输入手机号" />
				</uni-forms-item>

				<uni-forms-item label="用户角色" name="role">
					<uni-data-select v-model="formData.role" :localdata="roleOptions" placeholder="请选择用户角色" />
				</uni-forms-item>
			</uni-forms>

			<template v-slot:actions>
				<view class="actions">
					<button class="action-btn" @click="handleSubmit" :loading="loading">
						创建
					</button>
					<button class="action-btn cancel" @click="goBack">
						取消
					</button>
				</view>
			</template>
		</uni-card>
	</view>
</template>

<script>
	import {
		request
	} from '@/utils/request';

	export default {
		data() {
			return {
				loading: false,
				formData: {
					username: '',
					password: '',
					email: '',
					phone: '',
					role: ''
				},
				roleOptions: [{
						value: 'ADMIN',
						text: '管理员'
					},
					{
						value: 'MAINTAINER',
						text: '维护人员'
					},
					{
						value: 'USER',
						text: '普通用户'
					}
				],
				rules: {
					username: {
						rules: [{
							required: true,
							errorMessage: '请输入用户名'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入密码'
						}, {
							minLength: 6,
							errorMessage: '密码长度不能少于6位'
						}]
					},
					email: {
						rules: [{
							required: true,
							errorMessage: '请输入邮箱'
						}, {
							format: 'email',
							errorMessage: '邮箱格式不正确'
						}]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '请输入手机号'
						}, {
							pattern: /^1[3-9]\d{9}$/,
							errorMessage: '手机号格式不正确'
						}]
					},
					role: {
						rules: [{
							required: true,
							errorMessage: '请选择用户角色'
						}]
					}
				}
			};
		},

		methods: {
			async handleSubmit() {
				try {
					const valid = await this.$refs.form.validate();
					if (!valid) return;

					this.loading = true;
					await request({
						url: '/api/users',
						method: 'POST',
						data: this.formData
					});

					uni.showToast({
						title: '创建成功',
						icon: 'success'
					});

					setTimeout(() => {
						uni.navigateBack();
					}, 1500);

				} catch (err) {
					uni.showToast({
						title: err.message || '创建失败',
						icon: 'none'
					});
				} finally {
					this.loading = false;
				}
			},

			goBack() {
				uni.navigateBack();
			}
		}
	};
</script>

<style>
	.user-create {
		padding: 15px;
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	.actions {
		display: flex;
		justify-content: flex-end;
		gap: 10px;
		margin-top: 20px;
	}

	.action-btn {
		font-size: 14px;
		padding: 4px 12px;
		border-radius: 4px;
		background-color: #007AFF;
		color: #fff;
		min-width: 60px;
	}

	.action-btn.cancel {
		background-color: #8e8e93;
	}
</style>